<template>
  <transition name="fade">
    <div class="register-wrapper" v-if="isRegisterVisible">
      <div class="tech-bg"></div>
      <a-card class="register-container">
        <div class="register-content">
          <h2 class="register-title">
            <span class="tech-glitch">注册</span>
            <div class="subtitle">CREATE ACCOUNT</div>
          </h2>
          <a-form
            layout="horizontal"
            :model="formState"
            @finish="handleRegister"
            @finishFailed="handleRegisterFailed"
          >
            <!-- 用户名输入框 -->
            <a-form-item>
              <a-input
                v-model:value="formState.userAccount"
                placeholder="Username"
                class="tech-input"
              >
                <template #prefix><UserOutlined class="input-icon" /></template>
              </a-input>
            </a-form-item>
            <!-- 密码输入框 -->
            <a-form-item>
              <a-input
                v-model:value="formState.userPassword"
                type="password"
                placeholder="Password"
                class="tech-input"
              >
                <template #prefix><LockOutlined class="input-icon" /></template>
              </a-input>
            </a-form-item>
            <!-- 确认密码输入框 -->
            <a-form-item>
              <a-input
                v-model:value="formState.checkPassword"
                type="password"
                placeholder="Confirm Password"
                class="tech-input"
              >
                <template #prefix><LockOutlined class="input-icon" /></template>
              </a-input>
            </a-form-item>
            <!-- 注册按钮 -->
            <a-form-item class="register-btn">
              <a-button type="primary" html-type="submit" size="large" class="tech-button">
                注册
              </a-button>
            </a-form-item>
            <!-- 返回登录按钮 -->
            <a-form-item class="login-link">
              <a-button type="link" @click="goToLogin"> 已有账号？登录 </a-button>
            </a-form-item>
          </a-form>
        </div>
      </a-card>
    </div>
  </transition>
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue'
import { UserOutlined, LockOutlined } from '@ant-design/icons-vue'
import { message } from 'ant-design-vue'
import { ValidateErrorEntity } from 'ant-design-vue/es/form/interface'
import router from '@/router'
import type { UnwrapRef } from 'vue'
import { userRegistryUsingPost } from '@/api/userController'

const formState: UnwrapRef<API.UserRegistryRequest & { checkPassword: string }> = reactive({
  userAccount: '',
  userPassword: '',
  checkPassword: '',
})

const isRegisterVisible = ref(true)

const handleRegister = async () => {
  if (formState.userPassword !== formState.checkPassword) {
    message.error('密码和确认密码不一致')
    return
  }
  try {
    console.log('注册请求参数：', formState)
    // 假设有一个注册 API
    const res = await userRegistryUsingPost(formState)
    if (res.data.code === 0) {
      message.success('注册成功')
      // 注册成功后跳转到登录页面
      router.push('/user/login')
    } else {
      message.error('注册失败：' + res.data.message)
    }
  } catch (error) {
    message.error('注册失败:' + error)
  }
}

const handleRegisterFailed = (errors: ValidateErrorEntity<API.UserRegistryRequest>) => {
  console.log(errors)
}

const goToLogin = () => {
  router.push('/user/login')
}
</script>

<style scoped>
.register-wrapper {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  background: #f0f2f5;
  overflow: hidden;
}

.register-container {
  width: 480px;
  backdrop-filter: blur(10px);
  background: rgba(255, 255, 255, 0.9) !important;
  border: 1px solid rgba(79, 70, 229, 0.1);
  box-shadow: 0 0 30px rgba(79, 70, 229, 0.15);
  border-radius: 20px;
  z-index: 1;
  transition: all 0.3s ease;
}

.register-content {
  padding: 20px;
}

.register-title {
  text-align: center;
  color: #1e293b;
  font-size: 2.5em;
  margin-bottom: 1.5em;
  text-transform: uppercase;
  font-weight: 700;
  letter-spacing: 2px;
}

.subtitle {
  font-size: 0.4em;
  color: #4f46e5;
  letter-spacing: 5px;
  margin-top: 5px;
}

.tech-input {
  background: rgba(255, 255, 255, 0.9) !important;
  border: 2px solid rgba(79, 70, 229, 0.2) !important;
  height: 45px;
  border-radius: 8px;
}

.tech-input:hover,
.tech-input:focus {
  border-color: #4f46e5 !important;
  box-shadow: 0 0 15px rgba(79, 70, 229, 0.2) !important;
}

.input-icon {
  color: #4f46e5 !important;
}

.tech-button {
  background: linear-gradient(45deg, #4f46e5, #06b6d4) !important;
  border: none !important;
  height: 45px;
  width: 100%;
  font-size: 1.1em;
  letter-spacing: 2px;
  position: relative;
  overflow: hidden;
  transition: all 0.3s ease;
}

.tech-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 20px rgba(79, 70, 229, 0.3);
}

.login-link {
  text-align: center;
  margin-top: 10px;
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.5s;
}
.fade-enter, .fade-leave-to /* .fade-leave-active in <2.1.8 */ {
  opacity: 0;
}

.tech-bg {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, #ffffff, #f6f9fc);
  z-index: 0;
}

.tech-bg::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(45deg, transparent 48%, #4f46e5 50%, transparent 52%),
    linear-gradient(-45deg, transparent 48%, #06b6d4 50%, transparent 52%);
  background-size: 60px 60px;
  animation: backgroundMove 30s linear infinite;
  opacity: 0.05;
}

.register-container:hover {
  box-shadow: 0 0 50px rgba(79, 70, 229, 0.2);
  transform: translateY(-5px);
}

.tech-glitch {
  position: relative;
  display: inline-block;
  color: #1e293b;
}

.tech-glitch::before,
.tech-glitch::after {
  content: '注册';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

.tech-glitch::before {
  left: 2px;
  text-shadow: -2px 0 #4f46e5;
  clip: rect(24px, 550px, 90px, 0);
  animation: glitch-anim 3s infinite linear alternate-reverse;
}

.tech-glitch::after {
  left: -2px;
  text-shadow: -2px 0 #06b6d4;
  clip: rect(85px, 550px, 140px, 0);
  animation: glitch-anim 2s infinite linear alternate-reverse;
}

@keyframes glitch-anim {
  0% {
    clip: rect(42px, 9999px, 44px, 0);
  }
  100% {
    clip: rect(35px, 9999px, 115px, 0);
  }
}

@keyframes backgroundMove {
  0% {
    background-position: 0 0;
  }
  100% {
    background-position: 100% 100%;
  }
}

:deep(.ant-input) {
  background: transparent !important;
  color: #1e293b !important;
}

:deep(.ant-input::placeholder) {
  color: rgba(30, 41, 59, 0.5) !important;
}

:deep(.ant-card-body) {
  padding: 0;
}
</style>
